package cn.tedu.sp02.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.sp01.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
@Slf4j
@RestController
public class ItemController {
    @Autowired
    private ItemService itemService;
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        List<Item> items = itemService.getItems(orderId);

        //模拟阻塞运算
        if (Math.random()<0.5){//90%概率执行阻塞代码
            //阻塞时长随机0-5秒
            int t = new Random().nextInt(2000);
            log.info("阻塞："+t);
            Thread.sleep(t);
        }
        return JsonResult.build().code(200).data(items);
        //return JsonResult.build(200,items);
    }

    /**
     * @RequestBody 客户端提交的数据，放在post请求协议体中
     * 服务器从提交的http协议体，完整接收商品列表数据
     * */
    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        itemService.decreaseNumbers(items);

        return JsonResult.build().code(200).msg("减少库存成功");
        //return JsonResult.build(200,"减少库存成功");
    }
    //处理浏览器自动发起图标文件请求，避免其他路径干扰路径
    @GetMapping("/favicon.ico")
    public void ico(){

    }

}
